import os

from xjlibrary.database_moudel.simple.mysqlclient import MySqlDbConnect, SelctSqlFromDB, ExeSqlToDB, ExeSqlList
from xjlibrary.mprocesspoll.MThreadingRun import MThreadingRun
from xjlibrary.mrequest.baserequest import MProxyRequest
from xjlibrary.our_file_dir.base_dir import BaseDir
from xjlibrary.tools.BaseUrl import BaseUrl

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "dqlib", "download", "issue")

HEADERS = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "accept-encoding": "gzip, deflate",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
    "upgrade-insecure-requests": "1",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "Host": "dqlib.vip.qikan.com",
    "Proxy-Connection": "keep-alive",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 "
                  "Safari/537.36 "
}

baseurl = "http://dqlib.vip.qikan.com/text/"


def requestVolIssue(url, proxysList):
    BoolResult, errString, r = MProxyRequest(url, Feature="facets-container", HEADERS=HEADERS, proxyRingList=proxysList,
                                             timeout=(30, 60))
    if not BoolResult:
        print("请检查失败原因:" + errString)
        return False
    return r


def savefile(r, filePath):
    BaseDir.single_write_file(r.text, filePath)


# 数据库链接
def MajorDbConnect():
    return MySqlDbConnect(curPath, "db.ini")


def SelectFromDB():
    sql = "select `url` from `issue` where `stat`=0"
    conn = MajorDbConnect()
    rows = SelctSqlFromDB(sql, conn)
    return rows


def UpdateSql(sql):
    conn = MajorDbConnect()
    ExeSqlToDB(sql, conn, errExit=True)


def requestJournals(url,proxysList):
    BoolResult, errString, r = MProxyRequest(url, Feature="maglist", HEADERS=HEADERS, proxyRingList=proxysList,
                                             timeout=(30, 60))
    if not BoolResult:
        print("请检查失败原因:" + errString)
        return ""
    return r

def SelectProxy():
    sSql = "SELECT `proxy` FROM `proxy_pool`"
    # 数据库连接
    conn = MajorDbConnect()
    rows = SelctSqlFromDB(sSql, conn)
    return rows




def get_list_url_run(result_queue,url,proxy_list):
    urlfull = baseurl + url
    dictUrl = BaseUrl.urlQuery2Dict(url)
    issn = dictUrl["issn"]
    year = dictUrl["year"]
    Issue = dictUrl["Issue"]
    filename = issn+"_"+year+"_"+Issue
    filePath = os.path.join(sPath, filename + ".html")
    if not os.path.exists(sPath):
        os.makedirs(sPath)
    r = requestJournals(urlfull, proxy_list)
    if r == "":
        sql = "update `issue` set `stat`=0,failed = failed + 1 where url='{}'".format(url)
        result_queue.put(sql)
        return
    try:
        savefile(r, filePath)
        sql = "update `issue` set `stat`=1 where url='{}'".format(url)
        result_queue.put(sql)
        print("保存文件成功")
    except:
        print("保存文件失败")


# 插入数据库
def InsertIntoDbFromList(ListSqls):
    conn = MajorDbConnect()
    success, failed = ExeSqlList(ListSqls, conn)
    ListSqls = list()


class IssueThreadRun(MThreadingRun):

    def __init__(self, num, func):
        super(IssueThreadRun, self).__init__(num, func)

    def setTask(self, *args, **kwargs):
        for row in SelectFromDB():
            self.add_job(self.func, row[0], self.list_proxy)
        # 开始线程
        self.thread_pool.start()

    def dealresult(self, *args, **kwargs):
        InsertIntoDbFromList(self.results)

    def setProxy(self, proxysList=None):
        list_proxy = []
        rows = SelectProxy()
        for proxy in rows:
            list_proxy.append(proxy[0])
        MThreadingRun.setProxy(self, list_proxy)


if __name__ == "__main__":
    func = get_list_url_run
    threadrun = IssueThreadRun(50, func)
    threadrun.setfunc(func)
    threadrun.run()